package com.jigsaw.demo.controller;

import com.jigsaw.demo.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Jigsaw on 2016/2/2.
 */

@Controller
@RequestMapping("/user")
public class UserController {
    private Map<String, User> users = new HashMap<>();

    public UserController() {
        users.put("sdy", new User("sdy", "123123", "asd@qq.com"));
        users.put("qq", new User("qq", "123123", "qq@qq.com"));
        users.put("ww", new User("ww", "123123", "ww@qq.com"));
    }
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model) {
        model.addAttribute("user", users);
        return "user/list";
    }

    @RequestMapping(value = "/lo", method = RequestMethod.GET)
    public String lo(Model model) {
        return "user/login";
    }

    //get 是显示表单
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(Model model) {
        //开启model driver
        model.addAttribute(new User());
        return "user/add";
    }

    //同样的方法是 接受表单的提交   如果一个表单不写action 那么他就是提交到 显示这个表单的action 也就是/add
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(@Validated User user, BindingResult br) {
        if (br.hasErrors()) {
            //注意这里不是 redirect  显示错误信息可以看 add.jsp
            return "/user/add";
        }
        users.put(user.getUsername(), user);
        return "redirect:/user/list";
    }

    //返回json数据 如果同样的URL 还需要返回JSP 那么就添加一个参数 在写一个 方法  这样 通过参数不同
    //来区分是 要JSON还是要JSP
    @RequestMapping(value = "/{username}", method = RequestMethod.GET)
    @ResponseBody
    public User show(@PathVariable String username) {
        return users.get(username);
    }

    @RequestMapping(value = "/{username}/update", method = RequestMethod.GET)
    public String update(@PathVariable String username, Model model) {
        model.addAttribute(users.get(username));
        return "/user/update";
    }

    @RequestMapping(value = "/{username}/delete", method = RequestMethod.GET)
    public String delete(@PathVariable String username) {
        users.remove(username);
        return "redirect:/user/list";
    }

    //注意这里保持session
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(String username, String password, HttpSession session) {
        if (!users.containsKey(username)) {
            throw new UserException("用户不存在");
        }
        User u = users.get(username);
        session.setAttribute("loginuser", u);
        return "redirect:/user/list";
    }

    //只能处理这个控制器的异常
//    @ExceptionHandler(value = {UserException.class})
//    public String handlerException(UserException e, HttpServletRequest req) {
//        req.setAttribute("e", e);
//        return "/user/error";
//    }


}
